﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Region.Forms
{

    public partial class ImportEgrip : Form
    {
        private Engine m_engine;
        Int32 m_KontID;
        public Int32 KontID { get { return m_KontID; } }
        void SelectPanel(Panel SelectPanel)
        {
            //Скрыть другие панели
            foreach (Control C in this.Controls)
                if (C is Panel)                
                    if (SelectPanel != C) 
                        (C as Panel).Visible = false;
            SelectPanel.Parent = this;
            SelectPanel.Left = 0;
            SelectPanel.Top = 0;
            SelectPanel.Visible = true;
            SelectPanel.Dock = DockStyle.None;
            // Пересчитать размер формы        
            this.Width = 10;
            this.Height = 10;
            this.AutoSize = true;
            SelectPanel.Dock = DockStyle.Fill;
        }
        public ImportEgrip(Engine engine)
        {
            m_engine = engine;
            m_KontID = 0;
            InitializeComponent();
            SelectPanel(Page1);
        }
        private void ImportEgrip_Load(object sender, EventArgs e)
        {

        }
        private void label6_Click(object sender, EventArgs e)
        {

        }
        private void Next1_Click(object sender, EventArgs e)
        {
            Dictionary<String, Object> P = new Dictionary<String, Object>();
            if(FindINN.Text.Trim().Length==10 || FindINN.Text.Trim().Length==12)  P["INN"] = FindINN.Text;
            if(FindOGRN.Text.Trim().Length==13 || FindOGRN.Text.Trim().Length==15) P["OGRN"] = FindOGRN.Text;
            if(FindName.Text.Trim().Length>2) P["FindName"] = FindName.Text;
            if(FindAddress.Text.Trim().Length>2) P["FindAddress"] = FindAddress.Text;
            List<KontList.EGRLine> List = KontList.getListEGRUL(m_engine, P);
            List.AddRange(KontList.getListEGRIP(m_engine, P));
            this.FindList.Items.Clear();
            foreach (KontList.EGRLine line in List)
            {
                ListViewItem R = new ListViewItem(line.ToString());
                R.Tag = line;
                R.SubItems.Add(line.INN);                
                R.SubItems.Add(line.KontName);
                R.SubItems.Add(Engine.getAddressString("", line.Region, line.Region, line.Gorod, line.Naspunkt, line.Street, line.Dom, line.Korpus, line.Office));

                if (line.INN.Length == 10) 
                    R.Group = this.FindList.Groups["UL"];
                if (line.INN.Length == 12) 
                    R.Group = this.FindList.Groups["IP"];
                this.FindList.Items.Add(R);
            }
            SelectPanel(Page2);
        }

        private void Back2_Click(object sender, EventArgs e)
        {
            SelectPanel(Page1);
        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void Next2_1_Click(object sender, EventArgs e)
        {
            KontName.Text = "";
            INN.Text = "";
            OGRN.Text = "";
            RegionEdit.Text = "";
            Raion.Text = "";
            Gorod.Text = "";
            Naspunkt.Text = "";
            Street.Text = "";
            Dom.Text = "";
            Korpus.Text = "";
            Office.Text = "";
            GosReg.Value = DateTime.Now;
            SelectPanel(Page3);
        }

        private void Next2_Click(object sender, EventArgs e)
        {
            try
            {
                KontList.EGRLine line = (KontList.EGRLine)this.FindList.SelectedItems[0].Tag;
                KontName.Text = line.KontName;
                INN.Text = line.INN;
                OGRN.Text = line.OGRN;
                RegionEdit.Text = line.Region;
                Raion.Text = line.Raion;
                Gorod.Text = line.Gorod;
                Naspunkt.Text = line.Naspunkt;
                Street.Text = line.Street;
                Dom.Text = line.Dom;
                Korpus.Text = line.Korpus;
                Office.Text = line.Office;
                GosReg.Value = line.GosReg;
                SelectPanel(Page3);
            }
            catch{}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SelectPanel(Page2);
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            KontList.EGRLine line = new KontList.EGRLine();
            line.KontName = KontName.Text;
            line.INN = INN.Text;
            line.OGRN = OGRN.Text;
            line.Region = RegionEdit.Text;
            line.Raion = Raion.Text;
            line.Gorod = Gorod.Text;
            line.Naspunkt = Naspunkt.Text;
            line.Street = Street.Text;
            line.Dom = Dom.Text;
            line.Korpus = Korpus.Text;
            line.Office = Office.Text;
            line.GosReg = GosReg.Value;
            if (!Kont.checkINN(line.INN)) { MessageBox.Show("Неправильно введён ИНН", "Ошибка при вводе", MessageBoxButtons.OK); return; }
            if (!Kont.checkOGRN(line.OGRN)) { MessageBox.Show("Неправильно введён ОГРН", "Ошибка при вводе", MessageBoxButtons.OK); return; }
            if (line.GosReg<new DateTime(1990,1,1)) { MessageBox.Show("Неправильно введена дата гос. регистрации", "Ошибка при вводе", MessageBoxButtons.OK); return; }
            m_KontID = KontList.ImportEGR(m_engine, line);
            Close();
        }

        private void Region_TextChanged(object sender, EventArgs e)
        {
            Address.Text = Engine.getAddressString("", RegionEdit.Text, Raion.Text, Gorod.Text, Naspunkt.Text, Street.Text, Dom.Text, Korpus.Text, Office.Text);
        }
    }
}
